package com.appstore.service.impl;


import com.appstore.mapper.BrowsingHistoryMapper;
import com.appstore.domain.entity.BrowsingHistory;
import com.appstore.service.BrowsingHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {

    @Resource
    private BrowsingHistoryMapper browsingHistoryMapper;

    @Override
    public List<BrowsingHistory> getBrowsingHistoryList(Long userId) {
        return browsingHistoryMapper.getBrowsingHistoryList(userId);
    }

    @Override
    public Integer deleteBrowsingHistory(Long id) {
        return browsingHistoryMapper.deleteBrowsingHistory(id);
    }

    @Override
    public Integer addBrowsingHistory(BrowsingHistory browsingHistory) {

        List<BrowsingHistory> browsingHistoryList = browsingHistoryMapper.getBrowsingHistoryList(browsingHistory.getUserId());
        for (BrowsingHistory history : browsingHistoryList) {
            if(history.getAppId().equals(browsingHistory.getAppId())) {
                browsingHistoryMapper.deleteBrowsingHistory(history.getId());
            }
        }
        return browsingHistoryMapper.addBrowsingHistory(browsingHistory);
    }
}
